import { defineStore } from 'pinia';
import commonApi from '@/apis/common'
import { get } from 'lodash'

export const patientStore = defineStore('patient', {
	state: () => {
		return {
			data: {o:3},
			pastHistory: [],
			habit: []
		};
	},
	actions: {
		setData(data) {
			this.data = data
		},
		setPatientData(key, value) {
			this.data[key] = value
		},
		async getPastHistories() {
			if(this.pastHistory.length) return this.pastHistory
			const res = await commonApi.getSettingList('newPastHistory')
			let other = get( res, 'result.0.other', '' );
			try {
				other = JSON.parse( other );
			} catch {
				other = [];
			}
			this.pastHistory = other.map(( v ) => {
				v.name = get( v, `lang.cn`, '' );
				return v;
			});
			return this.pastHistory
		},
		async getHabits() {
			if(this.habit.length) return this.habit
			const res = await commonApi.getSettingList('newOralHabits')
			let other = get( res, 'result.0.other', '' );
			try {
				other = JSON.parse( other );
			} catch {
				other = [];
			}
			this.habit = other.map(( v ) => {
				v.name = get( v, `lang.cn`, '' );
				return v;
			});
			return this.habit
		}
	}
});
